package com.holyn.guofang.view.distinguish;

import android.app.Activity;
import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.Button;

import com.holyn.guofang.R;
import com.holyn.guofang.model.constant.AlgorithmTypeConstant;
import com.holyn.guofang.model.webhttp.wo.AlgorismWo;
import com.holyn.guofang.view.BaseActivity;
import com.holyn.guofang.viewcommon.tab.TopTabRecyclerView;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Holyn on 2017/7/20.
 * <p>
 * 监听每个fragment的type与value的组装
 * setResult：type的list，value的list
 */

public class AlgorithmActivity extends BaseActivity {

    public static final String INTENT_KEY_TYPE_LIST = "INTENT_KEY_TYPE_LIST";

    @BindView(R.id.rv_top_tab)
    TopTabRecyclerView rvTopTab;
    @BindView(R.id.viewpager)
    ViewPager viewpager;
    @BindView(R.id.btn_submit)
    Button btnSubmit;

    private boolean isClickSubmit = false;

    private List<String> typeList;
    private List<String> typeNameList = new ArrayList<>();
    private Map<String, AlgorithmFragment> fragmentMap = new HashMap<>();
    private ViewPagerFragmentAdapter viewPagerFragmentAdapter;
    private List<AlgorithmFragment> fragmentList = new ArrayList<>();

    public static void startForResult(Activity activity, ArrayList<String> typeList, int requestCode) {
        Intent intent = new Intent(activity, AlgorithmActivity.class);
        intent.putStringArrayListExtra(INTENT_KEY_TYPE_LIST, typeList);
        activity.startActivityForResult(intent, requestCode);
    }

    @Override
    protected int getLayoutResID() {
        return R.layout.activity_algorithm;
    }

    @Override
    protected void onMyCreate() {
        ButterKnife.bind(this);
        initData();
        initView();
    }

    @Override
    public void onBackPressed() {
        if (!isClickSubmit){
            setResult(Activity.RESULT_OK, getIntent());
        }
        super.onBackPressed();
    }

    @Override
    protected void onTopLeftBackPressListener(View v) {
        if (!isClickSubmit){
            setResult(Activity.RESULT_OK, getIntent());
        }
        super.onTopLeftBackPressListener(v);
    }

    private void initData() {
        typeList = getIntent().getStringArrayListExtra(INTENT_KEY_TYPE_LIST);
//        curAlgorismTypeWo = AlgorithmTypeConstant.getAlgorismTypeWoByTypeList(typeList);
//        showToolbar(curAlgorismTypeWo.getType().get(0).getName());

        for (String type : typeList) {
            typeNameList.add(AlgorithmTypeConstant.getNameByType(type));

            AlgorithmFragment algorithmFragment = AlgorithmFragment.newInstance(type);
            fragmentMap.put(type, algorithmFragment);
            fragmentList.add(algorithmFragment);
        }

        showToolbar(typeNameList.get(0));

    }

    private void initView() {
        rvTopTab.setData(typeNameList, 0);
        rvTopTab.setOnItemSelectListener(new TopTabRecyclerView.OnItemSelectListener() {
            @Override
            public void OnItemSelect(int position) {
                viewpager.setCurrentItem(position, true);
            }
        });


        viewPagerFragmentAdapter = new ViewPagerFragmentAdapter(getSupportFragmentManager(), fragmentList);
        viewpager.setAdapter(viewPagerFragmentAdapter);

        viewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                rvTopTab.smoothMoveToPosition(position);
                showToolbar(typeNameList.get(position));
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        viewpager.setOffscreenPageLimit(typeList.size());
    }

    @OnClick(R.id.btn_submit)
    public void onViewClicked() {
        isClickSubmit = true;
        List<List<String>> algorithmTypeList = new ArrayList<>();
        for (AlgorithmFragment fragment: fragmentList){
            List<AlgorismWo.TypeBean> algorithmList = fragment.getSelect();
            List<String> algorithmCodeList = new ArrayList<>();
            for (AlgorismWo.TypeBean algorithm : algorithmList){
                algorithmCodeList.add(algorithm.getCode());
            }
            algorithmTypeList.add(algorithmCodeList);
        }
        Intent intent = getIntent();
        intent.putExtra(AlgorithmSelectActivity.INTENT_KEY_ALGORITHM_TYPE_LIST, (Serializable) algorithmTypeList);
        setResult(Activity.RESULT_OK, intent);
        AlgorithmActivity.this.finish();
    }

    private class ViewPagerFragmentAdapter extends FragmentStatePagerAdapter {
        private List<AlgorithmFragment> fragmentList = new ArrayList<>();

        public ViewPagerFragmentAdapter(FragmentManager fm, List<AlgorithmFragment> fragmentList) {
            super(fm);
            this.fragmentList = fragmentList;
        }

        @Override
        public Fragment getItem(int position) {
            return fragmentList.get(position);
        }

        @Override
        public int getCount() {
            return fragmentList.size();
        }
    }
}
